package arrAndstr;

import java.util.HashMap;
import java.util.Map;

/*
    290. 单词规律
 */
public class T290 {

    public static void main(String[] args) {
        String pattern = "jquery";
        String s = "jquery";

        System.out.println(wordPattern(pattern, s));
    }

    public static boolean wordPattern(String pattern, String s) {
        char[] chars = pattern.toCharArray();
        String[] split = s.split(" ");
        Map<Character, String> map = new HashMap<>();
        if (chars.length != split.length) {
            return false;
        }

        for (int i = 0; i < chars.length; i++) {
            if (!map.containsKey(chars[i]) && map.containsValue(split[i])) {
                return false;
            }
            if (!map.containsKey(chars[i])) {
                map.put(chars[i], split[i]);
            }
        }

        StringBuffer sb = new StringBuffer(split[0]);

        for (int i=1; i<chars.length; i++) {
            sb.append(" ");
            sb.append(map.get(chars[i]));
        }

        return sb.toString().equals(s);
    }
}
